﻿using System;
using System.Xml;
using Common.Utilities.Excel.Xml;
using System.Web;
using System.IO;
using System.Web.Caching;

namespace Common.Utilities.Excel.Import
{
    public class ExcelImportConfiguration
    {
        #region Fields

        /// <summary>
        /// 配置文件路径
        /// </summary>
        private const string ConfigFilePath = "App_Config/Excel/ExcelImport.config";


        private static FileSystemWatcher watcher;

        /// <summary>
        /// 将Excel导入的配置信息存储与Cahce中的Key名称
        /// </summary>
        public static string CacheKey = "Bingosoft.ExcelUtilities.ExcelImportConfig";
        private XmlDocument xmlDoc;
        private ExcelImportProviderCollection _excels;
        #endregion

        #region 构造方法
        static ExcelImportConfiguration()
        {
        }

        private ExcelImportConfiguration(XmlDocument doc)
        {
            this.xmlDoc = doc;
            this._excels = new ExcelImportProviderCollection();
            this.Initialize();
        }
        #endregion

        #region 方法定义
        private void Initialize()
        {
            XmlNode rootNode = this.GetConfigSection("/ImportConfigs");
            foreach (XmlNode childNode in rootNode.ChildNodes)
            {
                switch (childNode.Name.ToLower())
                {
                    case "excel":
                        // add by huangDH
                        if (!string.IsNullOrEmpty(XmlUtility.getNodeAttributeStringValue(childNode, "typename")))
                        {
                            ExcelImportProvider providerType = new ExcelImportProvider(childNode);
                            _excels.Add(providerType);
                        }
                        break;
                }
            }
        }

        /// <summary>
        /// 读取配置文件中指定的节点。
        /// </summary>
        /// <param name="nodePath">节点 XPath 路径。</param>
        /// <returns>返回指定的节点或 null 值。</returns>
        public XmlNode GetConfigSection(String nodePath)
        {
            return this.xmlDoc.SelectSingleNode(nodePath);
        }
        #endregion

        #region 属性定义
        public ExcelImportProviderCollection ExcelConfigs
        {
            get { return _excels; }
        }

        private static ExcelImportConfiguration excelImportConfiguration;
        /// <summary>
        /// 获取 WayGo Framework 配置类的唯一实例。
        /// </summary>
        /// <value>WayGo Framework 配置类的唯一实例。</value>
        public static ExcelImportConfiguration Current
        {
            get
            {
                if (excelImportConfiguration == null)
                {
                    String path = null;
                    XmlDocument doc = new XmlDocument();
                    HttpContext context = HttpContext.Current;
                    if (context != null)
                    {
                        path = context.Server.MapPath("~/" +　ConfigFilePath);
                    }
                    else
                    {
                        path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFilePath);
                    }
                    doc.Load(path);
                    excelImportConfiguration = new ExcelImportConfiguration(doc);

                    if (watcher == null)
                    {
                        watcher = new FileSystemWatcher(Path.GetDirectoryName(path), "ExcelImport.config");
                        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                        watcher.EnableRaisingEvents = true;
                    }
                }
                return excelImportConfiguration;
            }
        }

        static void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            excelImportConfiguration = null;
        }
        #endregion
    }
}
